//
//  YXWelcomeViewController.swift
//  YXweibo
//
//  Created by AS on 5/14/16.
//  Copyright © 2016 AS. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class YXWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        prepareUI()
        // 即使头像在本地缓存了图片,刚开始会用占位图片,只有网路请求完成后才会使用SDWebImage设置图片
        // 解决方法,在获取用户信息之前,也使用SDWebImage去设置一下头像的图片
        setIcon()
        // 获取用户信息
        YXUserAccountViewModel.sharedInstance.loadUserInfo { (error) in
            self.setIcon()
        }
        //moveAnimation()
    }
    private func setIcon(){
        if let avatar_large = YXUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
             // 获取到了用户数据,设置头像
            let url = NSURL(string: avatar_large)
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        // 在用户看的到界面的时候在调用动画
        moveAnimation()
    }
    // 移动动画
    private func moveAnimation()
    {
        // 弹簧动画
        // usingSpringWithDamping: 弹簧的明显程度 0 - 1数值越小弹簧效果越明显
        // initialSpringVelocity: 初始速度
       
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: UIViewAnimationOptions(rawValue:1), animations: {
            // 要做动画
            // snp_updateConstraints: 更新约束
            self.iconView.snp_updateConstraints(closure: { (make) in
                // 更新底部约束
                make.bottom.equalTo(self.view).offset(-(UIScreen.mainScreen().bounds.height - 160))
            })
             // 根据约束重新设置frame值
            self.view.layoutIfNeeded()
            }) { (_) in
                UIView.animateWithDuration(1, animations: { 
                    // 移动动画完成, 是label.alpha = 1
                    self.messageLabel.alpha = 1

                    }, completion: { (_) in
                        
                         // 切换到MainViewController
                        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
                        appDelegate.switchRootViewController(YXMainTabBarController())
            
                })
                
        }
    }
    private func prepareUI()
    {
        // self.view添加子控件
        view.addSubview(backgroundImageView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
        // autolayout
        // 背景
        // bkgView: 要添加约束的view
        // snp_makeConstraints: 要添加约束
        // make: 要添加约束的view
        backgroundImageView.snp_makeConstraints{(make)-> Void in
            
            // 填充父控件
            make.edges.equalTo(self.view)
        }
         // 头像
        iconView.snp_makeConstraints { (make) in
            // make.centerX: 要约束view的centerX属性
            // equalTo: 参照
            // self.view: 参照的view
            // snp_centerX: 参照View的哪个属性, 参照view的属性需要添加snp_
            //   make.centerX.equalTo(self.view.snp_centerX)
            // 当要约束的约束和参照的约束一样的时候,参照的约束可以省略
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-160)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
         // 消息label
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
    }
    
    //MARK:- 懒加载控件
    
    /**
     *  背景图片
     */
    private lazy var backgroundImageView:UIImageView = {
        let imageView = UIImageView(image:UIImage(named:"ad_background" ))
  
        return imageView
        
    }()
    /**
     *  用户头像
     */
    
    private lazy var iconView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        // 圆角
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
    /**
     *  消息label
     */
    private lazy var messageLabel:UILabel = {
       let lable = UILabel()
        // 设置label
        lable.text = "欢迎归来~"
        lable.textColor = UIColor.orangeColor()
        lable.font = UIFont.systemFontOfSize(15)
        lable.sizeToFit()
        lable.alpha = 0
        return lable
    }()
    
}
